package cn.com.blueInfo.bpm.template.service.impl;

import cn.com.blueInfo.bpm.template.convert.AutomationRuleConvert;
import cn.com.blueInfo.bpm.template.entity.AutomationRule;
import cn.com.blueInfo.bpm.template.repository.AutomationRuleRepository;
import cn.com.blueInfo.bpm.template.request.AutomationRuleDTO;
import cn.com.blueInfo.bpm.template.response.AutomationRuleVO;
import cn.com.blueInfo.bpm.template.service.AutomationRuleService;
import cn.com.blueInfo.core.util.SnowflakeIdGenerator;
import cn.com.blueInfo.utils.result.ResultInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 自动化规则表 业务服务实现类
 * @author suxch
 * @since 2025-09-01 19:30:44
 */
@Log4j2
@Service
public class AutomationRuleServiceImpl implements AutomationRuleService {

    @Autowired
    private AutomationRuleRepository automationRuleRepository;

    @Autowired
    private SnowflakeIdGenerator snowflakeIdGenerator;

    @Autowired
    private AutomationRuleConvert automationRuleConvert;

    /**
     * 新增自动化规则
     * @param automationRuleDTO 自动化规则参数
     */
    @Override
    public ResultInfo<Integer> addAutomationRule(AutomationRuleDTO automationRuleDTO) {
        AutomationRule automationRule = automationRuleConvert.dtoToEntity(automationRuleDTO);
        automationRule.setId(snowflakeIdGenerator.nextId());
        Integer addSize = automationRuleRepository.createAutomationRule(automationRule);
        return ResultInfo.<Integer>builder().data(addSize).message("自动化规则成功新增 {} 条").build();
    }

    /**
     * 批量新增自动化规则
     * @param automationRuleDTOList 自动化规则参数列表
     */
    @Override
    public ResultInfo<Integer> batchAddAutomationRule(List<AutomationRuleDTO> automationRuleDTOList) {
        List<AutomationRule> automationRuleList = automationRuleConvert.dtoToEntity(automationRuleDTOList);
        for (AutomationRule automationRule : automationRuleList) automationRule.setId(snowflakeIdGenerator.nextId());
        Integer addSize = automationRuleRepository.batchCreateAutomationRule(automationRuleList);
        return ResultInfo.<Integer>builder().data(addSize).message("自动化规则成功新增 {} 条").build();
    }

    /**
     * 删除自动化规则
     * @param automationRuleId 自动化规则ID
     */
    @Override
    public ResultInfo<Integer> removeAutomationRule(Long automationRuleId) {
        Integer removeSize = automationRuleRepository.deleteAutomationRule(automationRuleId);
        return ResultInfo.<Integer>builder().data(removeSize).message("自动化规则成功删除 {} 条").build();
    }

    /**
     * 批量删除自动化规则
     * @param automationRuleIdList 自动化规则ID列表
     */
    @Override
    public ResultInfo<Integer> batchRemoveAutomationRule(List<Long> automationRuleIdList) {
        Integer removeSize = automationRuleRepository.batchDeleteAutomationRule(automationRuleIdList);
        return ResultInfo.<Integer>builder().data(removeSize).message("自动化规则成功删除 {} 条").build();
    }

    /**
     * 更新部分自动化规则
     * @param automationRuleUpdatePartialDTO 自动化规则参数
     */
    @Override
    public ResultInfo<Integer> modifyAutomationRulePartial(
            AutomationRuleDTO.UpdatePartialDTO automationRuleUpdatePartialDTO) {
        AutomationRule automationRule = automationRuleConvert.dtoToEntity(automationRuleUpdatePartialDTO);
        Integer modifySize = automationRuleRepository.updateAutomationRulePartial(automationRule);
        return ResultInfo.<Integer>builder().data(modifySize).message("自动化规则成功更新 {} 条").build();
    }

    /**
     * 更新全部自动化规则
     * @param automationRuleDTO 自动化规则参数
     */
    @Override
    public ResultInfo<Integer> modifyAutomationRule(AutomationRuleDTO automationRuleDTO) {
        AutomationRule automationRule = automationRuleConvert.dtoToEntity(automationRuleDTO);
        Integer modifySize = automationRuleRepository.updateAutomationRule(automationRule);
        return ResultInfo.<Integer>builder().data(modifySize).message("自动化规则成功更新 {} 条").build();
    }

    /**
     * 查询一条自动化规则
     * @param automationRuleId 自动化规则ID
     */
    @Override
    public ResultInfo<AutomationRuleVO> queryAutomationRuleById(Long automationRuleId) {
        AutomationRule automationRule = automationRuleRepository.getAutomationRuleById(automationRuleId);
        AutomationRuleVO automationRuleVO = automationRuleConvert.entityToVO(automationRule);
        return ResultInfo.<AutomationRuleVO>builder().data(automationRuleVO).message("自动化规则查询成功").build();
    }

    /**
     * 分页查询自动化规则
     * @param automationRuleQueryDTO 自动化规则参数
     */
    @Override
    public ResultInfo<Page<AutomationRuleVO>> queryAutomationRulePage(
            AutomationRuleDTO.QueryDTO automationRuleQueryDTO) {
        Page<AutomationRule> automationRulePage =
                automationRuleRepository.getAutomationRulePage(automationRuleQueryDTO);
        Page<AutomationRuleVO> resultData = automationRuleConvert.convertPage(automationRulePage);
        return ResultInfo.<Page<AutomationRuleVO>>builder()
                .data(resultData)
                .message("自动化规则分页查询成功，共 {} 条", resultData.getRecords().size())
                .build();
    }

    /**
     * 根据条件查询 自动化规则 数量
     * @param automationRuleQueryDTO 自动化规则参数
     */
    @Override
    public ResultInfo<Long> queryAutomationRuleCount(AutomationRuleDTO.QueryDTO automationRuleQueryDTO) {
        Long resultCount = automationRuleRepository.getAutomationRuleCount(automationRuleQueryDTO);
        return ResultInfo.<Long>builder()
                .data(resultCount)
                .message("自动化规则数量查询成功，共 {} 条", resultCount)
                .build();
    }

    /**
     * 根据条件查询 自动化规则
     * @param automationRuleQueryDTO 自动化规则参数
     */
    @Override
    public ResultInfo<List<AutomationRuleVO>> queryAutomationRuleList(
            AutomationRuleDTO.QueryDTO automationRuleQueryDTO) {
        List<AutomationRule> automationRuleList =
                automationRuleRepository.getAutomationRuleList(automationRuleQueryDTO);
        List<AutomationRuleVO> resultData = automationRuleConvert.entityListToVoList(automationRuleList);
        return ResultInfo.<List<AutomationRuleVO>>builder()
                .data(resultData)
                .message("自动化规则分页查询成功，共 {} 条", resultData.size())
                .build();
    }
}
